//
//  CPDViewController.m
//  PROJECT
//
//  Created by PROJECT_OWNER on TODAYS_DATE.
//  Copyright (c) TODAYS_YEAR PROJECT_OWNER. All rights reserved.
//

#import "ADSViewController.h"
#import <AssistLink/ADSEBLEManager.h>

@interface ADSViewController ()

@end

@implementation ADSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self autoConnectWiFiViaBLE];
}

- (void)autoConnectWiFiViaBLE{
    
    //BLE扫描回调
    [ADSEBLEManager shareInstance].scanBlock = ^(CBPeripheral * _Nonnull per, NSDictionary * _Nonnull advertisementData, NSNumber * _Nonnull RSSI) {
        NSLog(@"--> %@",per.name);
        if([per.name containsString:@"281950"]){
            [[ADSEBLEManager shareInstance] connectDeviceWithPeripheral:per];
        }

    };

    //BLE连接回调
    [ADSEBLEManager shareInstance].connectBLEBlock = ^(BOOL success) {
        [[ADSEBLEManager shareInstance] stopScan];//设备连接之后停止BLE扫描
        NSLog(@"BLE连接 --> %d ",success);
        if(success){
            [[ADSEBLEManager shareInstance] connectWiFi];
        }
    };
    
    //自动连接WiFi回调
    [ADSEBLEManager shareInstance].connectWiFiBlock = ^(BOOL success, NSString *ssid) {
        if(success){
            NSLog(@"加入WiFi成功");
            //建议延迟处理，加入WiFi后自动分配IP需要一定时间
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            });
        }
        else{
            NSLog(@"加入WiFi错误");
        }
    };
    
    //开始BLE扫描
    [[ADSEBLEManager shareInstance] startScan];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

